<?php
/**
 * vanephp web framework
 * 调度器工具类
 *
 * @author aray <ofree@qq.com>
 * @link http://www.vanephp.com/
 * @copyright Copyright &copy; 2010 aray
 * @license http://www.vanephp.com/license
 * @package core
 */

if ( ! defined ( 'INVANE' ) )
	exit ( '!' );

define ( 'CONTROLLER', dispatcher::getController ( ) );
define ( 'ACTION', dispatcher::getAction ( ) );

/**
 * 调度器工具类
 * @author aray
 * @abstract
 */
abstract class dispatcher {
	/**
	 * 当前控制器
	 * @var string
	 * @access privete
	 */
	private static $controller = null;
	/**
	 * 当前动作
	 * @var string
	 * @access private
	 */
	private static $action = null;
	/**
	 * 获取当前控制器
	 */
	final public static function getDispatcher ( ) {
		return vane::singleton ( config::get ( 'dispatcher_class' ), $_GET );
	}
	/**
	 * 进行调度
	 */
	final public static function dispatching ( ) {
		if ( ! self::getDispatcher ( )->check ( ) ) {
			call_user_func_array ( config::get ( 'dispatch_faile_callback' ), array (
				self::getController ( ), self::getAction ( ) 
			) );
			exit ( );
		}
		
		$actionMethod = config::get ( 'action_prefix' ) . self::getAction ( );
		$controller = self::loadController ( );
		
		if ( method_exists ( $controller, $actionMethod ) ) {
			
			# 国际化支持
			$VARS = array();
			$defaultLanguage = config::get ( 'default_language' );
			if ( config::get ( 'i18n_support' ) == true ) {
				if ( config::get ( 'auto_select_language' ) == true ) {
					$headerLang = explode ( ',', $_SERVER['HTTP_ACCEPT_LANGUAGE'] );
					if ( isset ( $headerLang[0] ) )
						config::set ( 'default_language', $headerLang[0] );
				}
				$i18nPath = config::get ( 'i18n_dir' ) . DS . $defaultLanguage;
				$VARS = vane::load ( $i18nPath, '无法加载[' . config::get ( 'languages', $defaultLanguage ) . ']语言包路径' );
			}
			view::setLanguage ( $defaultLanguage );
			
			# 合并控制器内定义的变量
			$controller->vars = array_merge ( $controller->vars, $VARS );
			
			$cacheHtmlData = '';
			
			# 读取缓存内容
			if ( view::isCached ( ) ) {
			
			} else {
				ob_start ( );
				$controller->$actionMethod ( );
				view::assign ( $controller->vars );
			}
			view::display ( );
		} else {
			trigger_error ( '找不到控制器 ' . self::getController ( ) . ' 动作 ' . self::getAction ( ), E_USER_ERROR );
		}
	}
	/**
	 * 获取当前控制器名称
	 */
	final public static function getController ( ) {
		return self::getDispatcher ( )->getController ( );
	}
	/**
	 * 获取当前动作名称
	 */
	final public static function getAction ( ) {
		return self::getDispatcher ( )->getAction ( );
	}
	/**
	 * 设置控制器名称
	 * @param string $controller	控制器名称
	 */
	final public static function setController ( $controller ) {
		self::$controller = $controller;
	}
	/**
	 * 设置动作名称
	 * @param string $action		动作名称
	 */
	final public static function setAction ( $action ) {
		self::$action = $action;
	}
	/**
	 * 转发到指定控制器，并执行动作
	 * @param string $controller	控制器
	 * @param string $action		动作
	 * @param boolean $forceExec	是否强制执行(如果为true，则直接执行该控制器，否则要手动执行dispatching()操作)
	 */
	final public static function forward ( $controller, $action, $forceExec = true ) {
		self::setController ( $controller );
		self::setAction ( $action );
		if ( $forceExec ) {
			self::getDispatcher ( )->dispatching ( );
		}
	}
	/**
	 * 获取ACL用户信息
	 */
	final public static function getUser ( ) {
		return isset ( $_SESSION[config::get ( 'session_key' )] ) ? $_SESSION[config::get ( 'session_key' )] : null;
	}
	/**
	 * 获取ACL用户权限信息
	 */
	final public static function getRole ( ) {
		$user = self::getUser ( );
		if ( $user ) {
			return isset ( $user[config::get ( 'rbac_key' )] ) ? $user[config::get ( 'rbac_key' )] : null;
		}
		return null;
	}
	/**
	 * 设置用户身份
	 * @param array $user
	 */
	final public static function setUser ( $user ) {
		$_SESSION[config::get ( 'session_key' )] = $user;
	}
	/**
	 * 设置用户权限
	 * @param array $role
	 */
	final public static function setRole ( $role ) {
		$user = self::getUser ( );
		if ( $user ) {
			$_SESSION[config::get ( 'session_key' )][config::get ( 'rbac_key' )] = $role;
		} else {
			trigger_error ( '用户身份不存在,无法设置用户权限!', E_USER_ERROR );
		}
	}
	/**
	 * 载入控制器
	 */
	private static function loadController ( ) {
		$controllerPath = config::get ( 'controller_classname_prefix' ) . '_' . self::getController ( );
		vane::load ( $controllerPath, '无法加载' . self::getController ( ) . '控制器' );
		$controller = vane::singleton ( $controllerPath );
		if ( $controller instanceof vane_controller ) {
			return $controller;
		}
		trigger_error ( '控制器 ' . self::getController ( ) . ' 不是 controller的实例', E_USER_ERROR );
	}
}